﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using SubSonic.DataProviders;
using SurveyHost.Support;
using System.Data.Linq;

namespace SurveyHost.Models
{
    public partial class Paragraph : BaseDataModel<Paragraph>
    {
        /// <summary>
        /// Creates a new <see cref="Paragraph"/>
        /// </summary>
        /// <param name="name">The (unique) name of the <see cref="Paragraph"/></param>
        /// <param name="text">The text of the <see cref="Paragraph"/></param>
        /// <param name="locale">The locale of the <see cref="Paragraph"/></param>
        /// <returns><see cref="Paragraph"/></returns>
        public static Paragraph Create(string name, string text, string locale)
        {
            try
            {
                using (var context = GetContext())
                {
                    Paragraph p = new Paragraph();
                    p.Name = name;
                    p.Text = text;
                    p.Locale = locale;
                    context.Paragraphs.InsertOnSubmit(p);
                    context.SubmitChanges(ConflictMode.FailOnFirstConflict);
                    return p;
                }
            }
            catch (ChangeConflictException cce)
            {
                throw new DataModelException("Unable to create the paragraph.  Name is not unique?", cce);
            }

        }
        /// <summary>
        /// Retrieves a <see cref="Paragraph"/> by its <paramref name="name"/>
        /// </summary>
        /// <param name="name">The <see cref="Paragraph"/> to retrieve</param>
        /// <returns><see cref="Paragraph"/> if found, <c>null</c> otherwise.</returns>
        public static Paragraph Retrieve(string name)
        {
            using (var context = GetContext())
            {
                return (from p in context.Paragraphs where p.Name == name select p).FirstOrDefault();
            }
        }
    }
}
